package com.mcxx.modules.miniSalvation.family.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.family.dao.write.MemberAssetsWriteDAO;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMemberEntity;
import com.mcxx.modules.miniSalvation.family.entity.MemberAssetsEntity;
import com.mcxx.modules.miniSalvation.family.service.read.MemberAssetsReadService;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@TaTransactional
public class MemberAssetsWriteService {
    @Autowired
    private MemberAssetsWriteDAO memberAssetsWriteDAO;



    public int deleteById(String id){
        int rst = memberAssetsWriteDAO.deleteById(id);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }
        return  rst;
    }


    public int deleteByMemberId(String memberId){
        int rst = memberAssetsWriteDAO.deleteByMemberId(memberId);
        return  rst;
    }

    public int deleteByOrderId(String orderId){
        int rst = memberAssetsWriteDAO.deleteByOrderId(orderId);
        return  rst;
    }

    public int deleteByLowerOrderId(String orderId){
        int rst = memberAssetsWriteDAO.deleteByLowerOrderId(orderId);
        return  rst;
    }

    public int insertIgnoreNull(MemberAssetsEntity entity, UserAccountVo userAccountVo){
        entity.setId(Constant.getUUID());
        entity.setCreateBy(userAccountVo.getUserId());
        int rst = memberAssetsWriteDAO.insertIgnoreNull(entity);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }
        return  rst;
    }


    public int updateIgnoreNull(MemberAssetsEntity entity){
        int rst = memberAssetsWriteDAO.updateIgnoreNull(entity);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }
        return  rst;
    }


    public int insertIgnoreNullByRecheck(MemberAssetsEntity entity){
        entity.setId(Constant.getUUID());
        int rst = memberAssetsWriteDAO.insertIgnoreNull(entity);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }
        return  rst;
    }


    public void saveBatchMemberAssetsTemp(List<MemberAssetsEntity> memberAssetsEntities){
        int rows = memberAssetsWriteDAO.insertBackupMemberAssets(memberAssetsEntities);
        if (rows < memberAssetsEntities.size()) {
            throw new BizException(FailureCode.ERR_9999);
        }
    }


    public int copyMemberAssetsInfo(String orderId,String newOrderId){
        return memberAssetsWriteDAO.copyMemberAssetsInfo(orderId,newOrderId);
    }

    public int insertLowerAssets(MemberAssetsEntity entity, UserAccountVo userAccountVo){
        entity.setId(Constant.getUUID());
        entity.setCreateBy(userAccountVo.getUserId());
        int rst = memberAssetsWriteDAO.insertLowerAssets(entity);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }
        return  rst;
    }
}
